package stark.app.function.chess;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.stark.chess.DefChessPlayActivity;
import com.stark.chess.endgame.ChessEndGame;
import com.stark.chess.endgame.ChessEndGameManager;
import com.stark.chess.endgame.ChessEndGameProvider;

import java.util.List;

import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.app.R;
import stark.app.databinding.FragmentFrameBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.event.IEventStat;
import stark.common.basic.utils.DialogUtil;
import stark.common.basic.utils.RxUtil;

/**
 * Author：daizhongbin on 2024/7/17
 * Email：1027154338@qq.com
 * Desc:
 */
public class ChessEndGameFragment extends BaseNoModelFragment<FragmentFrameBinding> {

    private ChessEndGameAdapter mEndGameAdapter;
    private int mSelPos = -1;

    @Override
    protected int onCreate() {
        return R.layout.fragment_frame;
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mDataBinding.frameRv.setLayoutManager(layoutManager);
        ChessEndGameAdapter endGameAdapter = new ChessEndGameAdapter();
        endGameAdapter.setOnItemClickListener(this);
        mEndGameAdapter = endGameAdapter;
        mDataBinding.frameRv.setAdapter(endGameAdapter);
    }

    @Override
    protected void initData() {
        loadEndGameDatas();
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (!ChessEndGameManager.getInstance().isUnlock(position)) {
            showUnlockDialog(position);
            return;
        }
        mSelPos = position;
        DefChessPlayActivity.start(getActivity(), mEndGameAdapter.getItem(position), position);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mSelPos >= 0 && mSelPos < mEndGameAdapter.getItemCount() - 1) {
            mEndGameAdapter.notifyItemChanged(mSelPos + 1);
        }
    }

    private void loadEndGameDatas() {
        showDialog(getString(com.stark.common.res.R.string.loading));
        RxUtil.create(new RxUtil.Callback<List<ChessEndGame>>() {
            @Override
            public void doBackground(ObservableEmitter<List<ChessEndGame>> emitter) {
                emitter.onNext(ChessEndGameProvider.getChessEndGames());
            }

            @Override
            public void accept(List<ChessEndGame> list) {
                dismissDialog();
                mEndGameAdapter.setNewInstance(list);
            }
        });
    }

    private void showUnlockDialog(int position) {
        String title = getString(com.stark.common.res.R.string.prompt);
        String content = "该关卡未解锁";
        String cancel = getString(com.stark.common.res.R.string.cancel);
        String confirm = "观看AD解锁";
        DialogUtil.asConfirm(getContext(), title, content, cancel, confirm, new OnConfirmListener() {
            @Override
            public void onConfirm() {
                EventStatProxy.getInstance().statEvent4(getActivity(), new IEventStat.IStatEventRewardCallback() {
                    @Override
                    public void onStatRewardCb(int rewardAmount) {
                        if (rewardAmount > 0) {
                            //获得激励，将该关卡设为解锁状态
                            ChessEndGameManager.getInstance().setUnlock(position);
                            mEndGameAdapter.notifyItemChanged(position);
                            mSelPos = position;
                            DefChessPlayActivity.start(getActivity(), mEndGameAdapter.getItem(position), position);
                        }
                    }

                    @Override
                    public void onStatOKCb() {
                    }
                });
            }
        }, null, false);
    }
}
